package com.chif.demo.producer;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
/**
 * 生产者单向发送消息
 */
public class OnewayProducer {

    public static void main(String[] args) throws Exception {
        //创建一个producer，参数为Producer Group名称
        DefaultMQProducer producer = new DefaultMQProducer("pg");

        producer.setNamesrvAddr("192.168.10.100:9876");

        //开启生产者
        producer.start();

        for (int i = 0; i < 100; i++) {
            byte[] body = ("Hi," + i).getBytes();
            Message msg = new Message("sinage", "someTag", body);
            //为消息指定key
            msg.setKeys("key-"+i);

            //单向发送消息
            producer.sendOneway(msg);
        }

        //关闭producer
        producer.shutdown();

    }
}
